<?php
/**
 * new project {ProjectName}
 * new todo {TodoSubject} {TodoDescript} for {ProjectID}
 */

X::import('Application.Modles.Command');

class CNew extends Command {

	public function parserParamString($paramString){
		$this->arguments = explode(' ', trim($paramString));
		foreach ( $this->arguments as $key => $value ){
			$this->arguments[$key] = trim($value);
		}
		$this->argumentsCount = count($this->arguments);
		
		return $this;
	}
	
	public function run(){
		if ( XUser::STATUS_OFFLINE  == X::app()->user->status ){
			$this->result->errno = XErrorCode::USER_OFFLINE;
			$this->result->error = 'User Offline.';
			return $this;	
		}
		
		$tableName = $this->arguments[0];
		switch ( $tableName ){
		case 'project' : 
			$itemNames = array('id', 'name', 'owner');
			$newRecord = array('', $this->arguments[1], X::app()->user->account);
			break;
		case 'todo':
			$itemNames = array('id', 'subject', 'describe', 'project', 'owner');
			$newRecord = array('',
				$this->arguments[1], 
				$this->arguments[2], 
				$this->arguments[4],
				X::app()->user->account);
			break;
		default:
			$this->result->error = 'Not support this command yet.';
			$this->result->errno = XErrorCode::WRONG_ARGUMENT; 
			return $this;
		}
		
		$db = new Database();
		$result = $db->SqlBuilder
			->insert()
			->into($tableName, $itemNames)
			->values(array($newRecord))
			->execute();
			
		if ( $result->hasError() ){
			$this->result->errno = XErrorCode::DATABASE;
			$this->result->error = $result->error();
		}
		
		return $this;
	}	
}
?>